package com.levi.springbootquartz.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

import java.util.Objects;

/**
 * @author shentong
 * @date 2021/7/16 21:45
 * @Description TODO
 */
@Configuration
public class QuartzConfig {

    /**
     * 配置jobDetail工厂组件，生成的JobDetail指向MyJob的execute()方法
     * @return
     */
    @Bean
    MethodInvokingJobDetailFactoryBean jobFactoryBean() {
        MethodInvokingJobDetailFactoryBean jobFactoryBean = new MethodInvokingJobDetailFactoryBean();
        jobFactoryBean.setTargetBeanName("myJob");
        jobFactoryBean.setTargetMethod("execute");
        return jobFactoryBean;
    }

    /**
     * 触发器工厂
     * @return
     */
    @Bean
    CronTriggerFactoryBean cronTrigger() {
        CronTriggerFactoryBean cronTrigger = new CronTriggerFactoryBean();
        cronTrigger.setCronExpression("0/10 * * * * ? ");//从0秒开始每10秒执行一次
        cronTrigger.setJobDetail(Objects.requireNonNull(jobFactoryBean().getObject()));
        return cronTrigger;
    }
}
